package cn.cai.service.impl;

import cn.cai.pojo.Parameter;
import cn.cai.pojo.QueryData;
import cn.cai.pojo.ResponseData;
import cn.cai.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String USERNAME;
    /**
     * 发送邮件到目标邮箱
     * @param mail 目标邮箱
     * @param parameter 要发送的内容
     */
    @Override
    public void sendEmali(String mail, Parameter parameter, List<QueryData> list) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置邮件主题
            helper.setSubject("报平安成功信息");
            // 设置发件人
            helper.setFrom(USERNAME);
            //设置收件人
            helper.setTo(mail);
            // 设置发送时间
            helper.setSentDate(new Date());
            // 使用模板引擎
            Context context = new Context();
            context.setVariable("parameter", parameter);
            context.setVariable("dataList",list);
            String process = templateEngine.process("mail.html", context);
            helper.setText(process,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送失败信息致目标邮箱
     * @param mail: 目标邮箱
     */
    @Override
    public void sendEmailForError(String mail) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置邮件主题
            helper.setSubject("报平安失败信息");
            // 设置发件人
            helper.setFrom(USERNAME);
            //设置收件人
            helper.setTo(mail);
            // 设置发送时间
            helper.setSentDate(new Date());
            // 使用模板引擎
            Context context = new Context();
            String process = templateEngine.process("mailError.html", context);
            helper.setText(process,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送重复上报信息到目标邮箱
     * @param mail : 目标邮箱
     * @param responseData 返回对象
     * @param list
     */
    @Override
    public void sendRepeat(String mail, ResponseData responseData, List<QueryData> list) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置邮件主题
            helper.setSubject("重复上报信息");
            // 设置发件人
            helper.setFrom(USERNAME);
            //设置收件人
            helper.setTo(mail);
            // 设置发送时间
            helper.setSentDate(new Date());
            // 使用模板引擎
            Context context = new Context();
            context.setVariable("responseData", responseData);
            context.setVariable("dataList",list);
            String process = templateEngine.process("mailRepeat.html", context);
            helper.setText(process,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
